<script>
	import Vue from 'vue'
	import store from '@/store'
	import {
		getToken,
	} from '@/utils/auth'
	import {
		setShareNumber,
	} from '@/utils/share'
	import {
		getCount
	} from '@/api/shoppingcart.js'
	import {
		mobileTheme
	} from '@/api/mobiletheme.js'
	export default {
		globalData: {
			shoppingCartCount: 0, //购物车数量
			theme: { // 主题
				themeColor: '', //主题颜色
				matchColor: '', //配色颜色
				bottomColor: '', //底色颜色
			},
			tabbarItems: [], // 底部导航
			tabbarStyle: { // 底部导航样式
				tabbarColor: '',
				color: '',
				selectedColor: '',
				backgroundColor: '',
				borderStyle: ''
			},
			isLoading: false, //防止多次请求
		},
		onLaunch: function() {
			// 小程序版本更新
			// #ifdef MP
			this.updateManager();
			// #endif
			//获取商城主题
			this.getMobileTheme();
			// 获取并设置购物车角标数量
			this.setTabBarBadge();

		},
		methods: {
			// 全局方法 
			initPage(options = {}) {
				if (options.isTabar) {
					this.setTabBar();
				}
				// 保存邀请人编号
				setShareNumber(options);
				return new Promise((resolve, reject) => {
					if (getToken()) {
						// #ifdef MP-WEIXIN
						wx.checkSession({
							success() {
								//session_key 未过期
								console.log("session_key未过期")
							},
							fail() {
								console.log("session_key已过期")
								// session_key 已经失效，需要重新登录
								store.dispatch("LoginWxMa").then((res) => {
									resolve("success");
								})
							}
						})
						// #endif
					}
					resolve("success");
				});
			},
			/**
			 * 获取主题
			 */
			getMobileTheme() {
				// 获取系统配置
				uni.getSystemInfo({
					success: function(e) {
						// #ifndef MP
						Vue.prototype.StatusBar = e.statusBarHeight;
						if (e.platform == 'android') {
							Vue.prototype.CustomBar = e.statusBarHeight + 50;
						} else {
							Vue.prototype.CustomBar = e.statusBarHeight + 45;
						};
						// #endif

						// #ifdef MP-WEIXIN
						Vue.prototype.StatusBar = e.statusBarHeight;
						let custom = wx.getMenuButtonBoundingClientRect();
						Vue.prototype.Custom = custom;
						Vue.prototype.CustomBar = custom.bottom + custom.top - e.statusBarHeight;
						// #endif		

						// #ifdef MP-ALIPAY
						Vue.prototype.StatusBar = e.statusBarHeight;
						Vue.prototype.CustomBar = e.statusBarHeight + e.titleBarHeight;
						// #endif

						// #ifdef MP-TOUTIAO
						Vue.prototype.StatusBar = e.statusBarHeight;
						let custom = uni.getMenuButtonBoundingClientRect();
						Vue.prototype.Custom = custom;
						Vue.prototype.CustomBar = custom.bottom + custom.top - e.statusBarHeight;
						// #endif
					}
				})
				let that = this;
				// 获取主题
				mobileTheme().then(res => {
					let themeObj = res.data;
					that.globalData.theme.themeColor = themeObj.themeColor
					that.globalData.theme.matchColor = themeObj.matchColor
					that.globalData.theme.bottomColor = themeObj.bottomColor
					that.globalData.tabbarStyle = {
						color: themeObj.tabbarColor ? themeObj.tabbarColor : '#666666',
						selectedColor: themeObj.tabbarSelectedColor ? themeObj.tabbarSelectedColor : '#f9002c',
						backgroundColor: themeObj.tabbarBgColor ? themeObj.tabbarBgColor : '#ffffff',
						borderStyle: themeObj.tabbarBorderStyle ? themeObj.tabbarBorderStyle : 'white'
					}
					if (themeObj.tabbarItem) {
						let tabbars = []
						themeObj.tabbarItem.info.forEach(item => {
							tabbars.push({
								index: item.index,
								text: item.text,
								iconPath: 'static/image/' + item.iconPath,
								selectedIconPath: 'static/image/tabbar/' + item
									.selectedIconPath
							})
						})
						that.globalData.tabbarItems = tabbars
					}
					this.setTabBar();
				});
			},
			/**
			 * 设置tabbar
			 */
			setTabBar() {
				let tabbarItems = this.globalData.tabbarItems;
				tabbarItems.forEach(item => {
					uni.setTabBarItem({
						index: item.index,
						text: item.text,
						iconPath: item.iconPath,
						selectedIconPath: item.selectedIconPath
					});
				})
				let tabbarStyle = this.globalData.tabbarStyle;
				// 设置tabbar样式
				uni.setTabBarStyle({
					color: tabbarStyle.color ? tabbarStyle.color : '#666666',
					selectedColor: tabbarStyle.selectedColor ? tabbarStyle
						.selectedColor : '#f9002c',
					backgroundColor: tabbarStyle.backgroundColor ? tabbarStyle
						.backgroundColor : '#ffffff',
					borderStyle: tabbarStyle.borderStyle ? tabbarStyle
						.borderStyle : 'white'
				});

			},
			/**
			 * 设置购物车角标
			 */
			setTabBarBadge() {
				//查询购物车数量
				getCount().then(res => {
					this.globalData.shoppingCartCount = res.data
					if (this.globalData.shoppingCartCount > 0) {
						uni.setTabBarBadge({
							index: 2,
							text: this.globalData.shoppingCartCount + ""
						})
					} else {
						uni.removeTabBarBadge({
							index: 2
						})
					}
				})


			},
			// #ifdef MP
			// 小程序版本更新
			updateManager() {
				const updateManager = uni.getUpdateManager();
				updateManager.onUpdateReady(function(res) {
					uni.showModal({
						title: '更新提示',
						content: '新版本已经准备好，是否重启应用？',
						success(res) {
							if (res.confirm) {
								// 新的版本已经下载好，调用 applyUpdate 应用新版本并重启
								updateManager.applyUpdate();
							}
						}
					});

				});
			}
			// #endif
		

		}
	}
</script>
<style lang="scss">
	@import "./components/colorui/main.css";
	@import "./components/colorui/icon.css";

	.hx-overflow-1 {
		display: -webkit-box; //谷歌
		-webkit-box-orient: vertical;
		-webkit-line-clamp: 1; //显示几行
		overflow: hidden;
	}

	.hx-overflow-2 {
		display: -webkit-box; //谷歌
		-webkit-box-orient: vertical;
		-webkit-line-clamp: 2; //显示几行
		overflow: hidden;
	}

	.bg-hx-green {
		background-color: #13d694;
		color: #fff;
	}

	.bg-hx-li-green {
		background-color: #313331;
		color: #fff;
	}

	.bg-hx-pink {
		background-color: #fe496e;
		color: #fff;
	}

	.bg-hx-li-pink {
		background-color: #ffe1e8;
		color: #fe496e;
	}

	.bg-hx-golden {
		background-color: #bb9c62;
		color: #fff;
	}

	.bg-hx-li-golden {
		background-color: #f1ebdd;
		color: #bb9c62;
	}

	.bg-hx-red {
		background-color: #f9002c;
		color: #fff;
	}

	.bg-hx-li-red {
		background-color: #ff8025;
		color: #fff;
	}

	.bg-hx-blue {
		background-color: #1c73f0;
		color: #fff;
	}

	.bg-hx-li-blue {
		background-color: #fe6220;
		color: #fff;
	}

	.text-hx-green {
		color: #13d694;
	}

	.text-hx-li-green {
		color: #313331;
	}

	.text-hx-pink {
		color: #fe496e;
	}

	.text-hx-li-pink {
		color: #ffe1e8;
	}

	.text-hx-golden {
		color: #bb9c62;
	}

	.text-hx-li-golden {
		color: #f1ebdd;
	}

	.text-hx-red {
		color: #f9002c;
	}

	.text-hx-li-red {
		color: #ff8025;
	}

	.text-hx-blue {
		color: #1c73f0;
	}

	.text-hx-li-blue {
		color: #fe6220;
	}

	.fixed {
		position: fixed;
		z-index: 99;
		width: 100%;
	}
</style>
